package com.example.shopspring.Controller;


import com.example.shopspring.Model.Cart;
import com.example.shopspring.Model.CartProduct;
import com.example.shopspring.Model.User;
import com.example.shopspring.Service.UserService;
import com.example.shopspring.util.ErrorResponse;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    //登录地址：http://localhost:8080/users/register
    @PostMapping("register")
    public ResponseEntity<?> register(@Valid @RequestBody User user,
                                   BindingResult result) {
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())
                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        ErrorResponse response=userService.register(user);
        if(response==null) return ResponseEntity.ok().body(user);
        return ResponseEntity.badRequest().body(response);
    }

    //登录地址：http://localhost:8080/users/login
    @PostMapping("login")
    public ResponseEntity<?> login(@Valid @RequestBody User user,
                                         BindingResult result) {
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())
                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        ErrorResponse response=userService.login(user);
        if(response==null) return ResponseEntity.ok().body(user);
        return ResponseEntity.badRequest().body(response);
    }

    //加入购物车地址：http://localhost:8080/users/addCart
    @PostMapping("addCart")
    public ResponseEntity<?> addCart(@Valid @RequestBody Cart cart,
                                     BindingResult result){
        if (result.hasErrors()) {
            // 收集所有的错误信息
            List<String> errorMessages = result.getAllErrors().stream()
                    .map(error -> error.getDefaultMessage())
                    .collect(Collectors.toList());
            // 返回包含所有错误信息的响应
            return ResponseEntity.badRequest().body(errorMessages);
        }
        ErrorResponse response=userService.addCart(cart);
        if(response==null) return ResponseEntity.ok().body(cart);
        return ResponseEntity.badRequest().body(response);
    }

    // 购物车列表：http://localhost:8080/users/getCartList
    @GetMapping("getCartList")
    public ResponseEntity<?> getCartList(String username) {
        List<CartProduct> cartProductList=userService.getCartList(username);
        return ResponseEntity.ok().body(cartProductList);
    }
    // 删除：http://localhost:8080/users/deleteCart
    @DeleteMapping("deleteCart")
    public ResponseEntity<?> deleteCart(@RequestParam int id) {
        ErrorResponse response=userService.deleteCart(id);
        if(response==null) return ResponseEntity.ok().build();
        return ResponseEntity.badRequest().body(response);
    }

    // 更新：http://localhost:8080/users/updateCart
    @PutMapping("updateCart")
    public ResponseEntity<?> updateCart(@RequestParam int id, @RequestParam int num) {
        ErrorResponse response=userService.updateCart(id,num);
        if(response==null) return ResponseEntity.ok().build();
        return ResponseEntity.badRequest().body(response);
    }

    //结算: http://localhost:8080/users/buyProducts
    @PostMapping("buyProducts")
    public ResponseEntity<?> buyProducts(@RequestBody Integer[] ids) {
        List<CartProduct> cartProducts=userService.getCartProducts(ids);
        ErrorResponse response=userService.buyProducts(cartProducts);
        if(response==null) return ResponseEntity.ok().build();
        return ResponseEntity.badRequest().body(response);
    }



}
